/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package k3tool.tinyex.filter;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import k3tool.tinyex.model.TableOfContentItem;

/**
 * A recurse data structure for generating e-Book under a designated directory.
 * 
 * @author Jeff Huang
 */
public class LocalDirectory {
    private final File origBase;
    private final File base;
    private boolean recursive;
    private int maxLevel; //<0 means unlimited, =0 means non recursive, be careful about the data insistency.
    private boolean caseSensitive;

    public LocalDirectory(final LocalDirectory parent, File base) {
        this(parent.origBase, base, parent.recursive, parent.maxLevel - 1);
        if(parent.recursive && parent.maxLevel == 1)
            this.recursive = false;
        this.caseSensitive = parent.caseSensitive;
    }


    public LocalDirectory(File origBase, File base, boolean recursive, int maxLevel) {
        this.origBase = origBase;
        this.base = base;
        this.recursive = recursive;
        this.maxLevel = maxLevel;
    }

    
    public LocalDirectory(File origBase, File base, boolean recursive) {
        this(origBase, base, recursive, -1);
    }


    public LocalDirectory(File origBase, File base) {
        this(origBase, base, false, -1);
    }

    public File getBase() {
        return base;
    }

    public int getMaxLevel() {
        return maxLevel;
    }

    public void setMaxLevel(int maxLevel) {
        this.maxLevel = maxLevel;
    }

    public boolean isRecursive() {
        return recursive;
    }

    public void setRecursive(boolean recursive) {
        this.recursive = recursive;
    }

    public boolean isCaseSensitive() {
        return caseSensitive;
    }

    public void setCaseSensitive(boolean caseSensitive) {
        this.caseSensitive = caseSensitive;
    }

    
    

    public List<TableOfContentItem> getAsToc(){
        List<TableOfContentItem> itemList = new LinkedList<TableOfContentItem>();

        File [] files = base.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                if(!recursive && !pathname.isFile())
                    return false;
                if(pathname.getName().startsWith("."))
                    return false;       //usually this is something people don't want to show
                return true;
            }
        });

        ArrayList<File> fileList = new ArrayList<File>();
        fileList.addAll(Arrays.asList(files));
        Collections.sort(fileList, new Comparator<File>() {

            @Override
            public int compare(File o1, File o2) {
                if(caseSensitive)
                    return o1.getName().compareTo(o2.getName());
                else
                    return o1.getName().toUpperCase().compareTo(o2.getName().toUpperCase());
            }

        });

        int counter = 0;
        for(File file : fileList){
            ++counter;
            TableOfContentItem item = FileName2Toc.makeItem(file, origBase);
            if(file.isDirectory() && recursive){
                LocalDirectory ld = new LocalDirectory(this, file);
                item.setSubItemList(ld.getAsToc());
            }
            itemList.add(item);
        }

        return itemList;
    }


}
